package com.xxxx.yoyo.adminweb;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Top;
import com.xxxx.yoyo.service.ITopService;
import com.xxxx.yoyo.service.impl.TopServiceImpl;


@WebServlet("/admin/topOperate")
public class TopOperateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ITopService topService = new TopServiceImpl();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取前端传递过米的参数operate,goodsId,type
		String operate = request.getParameter("operate");	//加入还是移除
		String goodsIdstr = request.getParameter("goodsId");
		int goodsId = Integer.parseInt(goodsIdstr);			//宠物的id
		String topTypestr = request.getParameter ("type");
		int topType = Integer.parseInt(topTypestr);			//1表示今日推荐（条幅） 2表示热销 3表示新品推荐
		//判断是添加还是删除操作
		if(operate.equals("join")){//把宠物加入榜单
			//创建top对象
			Top top = new Top(topType, goodsId);
			//把top添加到数据库中
			boolean isSuccess = topService.addTop(top);
			if(isSuccess){//添加成功
				//给前端返回ok
				response.getWriter().append ("ok");
			}else{//添加失败
				//给前端返回error
				response.getWriter().append ("error");
			}
		}else{//把宠物移除榜单
			//创建top对象
			Top top = new Top(topType,goodsId);
			//把top从数据库中删除
			boolean isSuccess = topService.deleteTop(top);
			if(isSuccess){//添加成功
				//给前端返回ok
				response.getWriter().append ("ok");
			}else{//添加失败
				//给前端返回error
				response.getWriter().append ("error");
			}
		}
	
	}

}
